
#ifndef _SERIAL_HEAD_
#define _SERIAL_HEAD_ 
 
#ifndef WIN32

#include  <termios.h>
#include  <unistd.h>
 //#include "plc.h" 
 
//#include <asm/termios.h>
 
class CSerial
{
public:
	CSerial() {
		m_fdCom = -1;
	}
	~CSerial() {
		if( m_fdCom > 0 )
		{
			CloseComPort( ); 
		}
	}
public:
	
	int OpenComPort (int ComPort, int baudrate, int databit, int stopbit, char parity );
	void CloseComPort ();

	int ReadComPort (void *data, int datalength );
	int ReadComPort_byte (unsigned char *data, int datalength);
	int ReadComPort_byte_ex (unsigned char *data, int datalength, unsigned char  start_tag,  int  timeoutSec = 3 );
	int ReadComPort_byte_plc(unsigned char *data, int datalength, unsigned char mode);
	int WriteComPort (unsigned char * data, int datalength );
private: 
	int    m_fdCom;	
	
	struct termios termios_old, termios_new;
	fd_set   fs_read, fs_write;
	struct timeval tv_timeout;
	
	void     set_baudrate (int);
	int    get_baudrate ();
	void     set_data_bit (int databit);
	int    baudrate2Bxx (int baudrate);
	int    Bxx2baudrate (int _baudrate);
	int    set_port_attr (
						int baudrate, 		//
						int databit,
						int stopbit, 
						int parity );
	int    set_port_attr_temp (
						int baudrate, 		//
						int databit,
						int stopbit, 
						int parity );
	void     set_stopbit (int stopbit);
	void     set_parity (int parity);
};

#endif

#endif
